using DataFrames
using QuadGK
using Distributions
using JLD
using Optim
using ForwardDiff
using CSV
using LinearAlgebra
using SpecialFunctions
using Random
using DelimitedFiles
using SparseArrays

clearconsole()

#-------------------------------------------------------------
# include Functions
#-------------------------------------------------------------
#cd("$(pwd())/Dropbox/Heterogeneity/Software/KS_Simulation/")
readDir = "$(pwd())/Functions/"
# include(readDir *"vech.jl");
#include(readDir *"VAR_Procedures.jl");
include(readDir *"VAR_MoreLags_Procedures.jl");
include(readDir *"Loaddata.jl");

#-------------------------------------------------------------
# choose specification files
#-------------------------------------------------------------
nVARMDDSpec = "1G"
specDir     = "$(pwd())/SpecFiles/"
include(specDir * "/AltVARMDDspec" * nVARMDDSpec * ".jl")

#-------------------------------------------------------------
# load aggregate and alt data
#-------------------------------------------------------------
dataDir  = "$(pwd())/data/"
agg_data, period_agg, ~ = loadaggdata(SampleStart,SampleEnd)
n_agg    = size(agg_data)[2]

percentiles_data     = readdlm("$(pwd())/data/percentiles_data.csv", ',', Float64, '\n'; skipstart = 1);
gini_coef_data       = readdlm("$(pwd())/data/gini_coef_data.csv", ',', Float64, '\n'; skipstart = 1);
probMass_below1_data = readdlm("$(pwd())/data/probMass_below1_data.csv", ',', Float64, '\n'; skipstart = 1);

period_pctl_ind = (SampleStart .<= percentiles_data[:,1] .<= SampleEnd)
period_gini_ind = (SampleStart .<= gini_coef_data[:,1] .<= SampleEnd)
period_Mass_ind = (SampleStart .<= probMass_below1_data[:,1] .<= SampleEnd)

percentiles_data = percentiles_data[period_pctl_ind,2:end]
gini_coef_data   = gini_coef_data[period_gini_ind,2:end]
probMass_below1_data   = probMass_below1_data[period_Mass_ind,2:end]

vec_percs = [0.1; 0.2; 0.5; 0.8; 0.9]
#-------------------------------------------------------------
# Combine agg and alt data
#-------------------------------------------------------------
if nSpecStr == "Pctl"
    data    = [ agg_data percentiles_data ]
    n_cross = size(percentiles_data)[2]
else
    data    = [ agg_data gini_coef_data probMass_below1_data]
    n_cross = 2
end
n = n_agg + n_cross

#-------------------------------------------------------------
# Compute MDD over hyperparameter grid
#-------------------------------------------------------------

MDD_vec = zeros(hyper_n)

for ll = 1:hyper_n

    # Extract hyperparaters
    lambda1 = hyper_grid[ll,1]
    lambda2 = hyper_grid[ll,2]
    lambda3 = hyper_grid[ll,3]
    lambda4 = 2
    lambda  = [lambda1 lambda2 lambda3 lambda4]
    nlags   = Int(hyper_grid[ll,4])

    # Adjust the sample
    data_adj = data[Tdrop-nlags+1:end,:]

    # OLS estimation
    YY, XX, PHIols, Sols, SIGMAols = data_to_YY(data_adj, nlags, nlags)

    # Generate Prior
    igammadf    = size(YY)[2]+sigdf
    prior       = prior_ACP_stru(YY, SIGMAols, n_agg, igammadf, nlags, lambda)

    # Compute MDD
    MDD_vec[ll] = ml_VAR_ACP(nlags,YY,XX,prior)

end


#-------------------------------------------------------------
# Save output
#-------------------------------------------------------------

sName    = "AltVAR_MDD" * nVARMDDSpec * "_" * nSpecStr
savedir  = "$(pwd())/results/" * sName *"/";
try mkdir(savedir) catch; end

lambda_MDD = [hyper_grid MDD_vec]
sNameCols  = ["Lam1", "Lam2", "Lam3", "Nlags", "MDD"]
sNameFile  = "AltVAR_MDD" * nVARMDDSpec * "_" * nSpecStr
CSV.write(savedir * sNameFile * "_lambda_MDD.csv", DataFrame(permutedims(sNameCols),:auto), header=false)
CSV.write(savedir * sNameFile * "_lambda_MDD.csv", DataFrame(lambda_MDD,:auto), append=true);
